import { getStorageToken } from '@/utils/storage'
// import ArticleView from '@/views/ArticleView.vue'
// import LayoutView from '@/views/LayoutView.vue'
// import LoginView from '@/views/LoginView.vue'
// import RegisterView from '@/views/RegisterView.vue'
// import CollectView from '@/views/tabbar/CollectView.vue'
// import HomeView from '@/views/tabbar/HomeView.vue'
// import LikeView from '@/views/tabbar/LikeView.vue'
// import UserView from '@/views/tabbar/UserView.vue'
import { createRouter, createWebHashHistory } from 'vue-router'
const ArticleView = () => import('@/views/ArticleView.vue')
const LoginView = () => import('@/views/LoginView.vue')
const RegisterView = () => import('@/views/RegisterView.vue')
const LayoutView = () => import('@/views/LayoutView.vue')
const HomeView = () => import('@/views/tabbar/HomeView.vue')
const CollectView = () => import('@/views/tabbar/CollectView.vue')
const LikeView = () => import('@/views/tabbar/LikeView.vue')
const UserView = () => import('@/views/tabbar/UserView.vue')

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/article/:id',
      component: ArticleView,
      meta: {
        title: '文章详情',
      },
    },
    {
      path: '/login',
      component: LoginView,
      meta: {
        title: '登录',
      },
    },
    {
      path: '/register',
      component: RegisterView,
      meta: {
        title: '注册',
      },
    },
    {
      path: '/',
      component: LayoutView,
      redirect: '/home',
      children: [
        {
          path: 'home',
          component: HomeView,
          meta: {
            title: '首页',
          },
        },
        {
          path: 'collect',
          component: CollectView,
          meta: {
            title: '收藏',
          },
        },
        {
          path: 'like',
          component: LikeView,
          meta: {
            title: '喜欢',
          },
        },
        {
          path: 'user',
          component: UserView,
          meta: {
            title: '我的',
          },
        },
      ],
    },
  ],
})
//路由白名单
const whiteList = ['/login', '/register']
router.beforeEach((to) => {
  console.log(to)
  if (to.meta.title) {
    document.title = to.meta.title + '|面试'
  }
  const token = getStorageToken()
  if (token) {
    return true
  } else {
    if (whiteList.includes(to.path)) {
      return true
    } else {
      return '/login'
    }
  }
})
export default router
